#include "blogitem.h"
#include "ui_blogitem.h"

BlogItem::BlogItem(QWidget *parent, QMap<QString, QString> *mapInfo):
    QWidget(parent),
    ui(new Ui::BlogItem)
{
    this->map = *mapInfo;
    //ui->setupUi(this);
    // 初始化变量
    this->blogTitle = map["blog_title"];
    this->blogOverview = map["blog_overview"];
    this->blogUser = map["blog_user"];
    this->blogPix = map["blog_pix"];
    this->blogTime = map["blog_time"];
    this->blogType = map["blog_type"];
    this->blogComment = map["blog_comment"];
    this->blogPraise = map["blog_praise"];
    this->blogCollection = map["blog_collection"];

    // 创建控件
    labelTitle = new QLabel(this->blogTitle);
    labelTitle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    labelContent = new QLabel(this->blogOverview);
    labelUser = new QLabel(this->blogUser);
    labelPix = new QLabel();
    QPixmap pix(blogPix); // 缩放
//    QPixmap dst = pix.scaled(40,40,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//    labelPix->setPixmap(dst);
    labelTime = new QLabel(this->blogTime);
    labelType = new QLabel(this->blogType);
    labelPraise = new QLabel(this->blogPraise);
    labelCollection = new QLabel(this->blogCollection);
    labelComment = new QLabel(this->blogComment);
    // 配置字体
    QFont font;
    // 加粗
    font.setBold(true);
    // 楷体
    font.setFamily("KaiTi");
    // 字号
    font.setPointSize(15);
    // 下划线
    font.setUnderline(true);

    labelTitle->setFont(font);
    font.setUnderline(false);
    font.setPointSize(12);
    labelContent->setFont(font);
    labelUser->setFont(font);
    labelTime->setFont(font);
    labelPraise->setFont(font);
    labelCollection->setFont(font);
    labelComment->setFont(font);


    QVBoxLayout *vLayout = new QVBoxLayout();
    // 第一行，标题
    vLayout->addWidget(labelTitle);
    // 第二行，内容提要
    vLayout->addWidget(labelContent);

    // 第三行，其他信息
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setMargin(0);
    QWidget *widget = new QWidget();
//    hLayout->addWidget(labelPix);
    QLabel *label11 = new QLabel(QString("作者:"));
    QLabel *label22 = new QLabel(QString("发布时间:"));
    QLabel *label33 = new QLabel(QString("文章板块:"));
    label11->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    label22->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");;
    label33->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    hLayout->addWidget(label11);
    hLayout->addWidget(labelUser);
    hLayout->addWidget(label22);
    hLayout->addWidget(labelTime);
    hLayout->addWidget(label33);
    hLayout->addWidget(labelType);
    QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);
    hLayout->addItem(spacer);
    labelPraisePix = new QLabel();
    QPixmap pix1(":/res/dispraise.jpeg"); // 缩放
    QPixmap dst = pix1.scaled(40,40,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelPraisePix->setPixmap(dst);
    hLayout->addWidget(labelPraisePix);
    hLayout->addWidget(labelPraise);
    labelCollectionPix = new QLabel();
    QPixmap pix2(":/res/discollection.jpeg"); // 缩放
    dst = pix2.scaled(40,40,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelCollectionPix->setPixmap(QPixmap(dst));
    hLayout->addWidget(labelCollectionPix);
    hLayout->addWidget(labelCollection);
    labelCommentPix = new QLabel();
    QPixmap pix3(":/res/comment.jpeg"); // 缩放
    dst = pix3.scaled(40,40,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelCommentPix->setPixmap(QPixmap(dst));
    hLayout->addWidget(labelCommentPix);
    hLayout->addWidget(labelComment);
    widget->setLayout(hLayout);
    vLayout->addWidget(widget);
    // 加载布局
    setLayout(vLayout);

    // 设置事件过滤器，监听鼠标点击
    labelTitle->installEventFilter(this);
    labelContent->installEventFilter(this);
    labelUser->installEventFilter(this);
    labelPraisePix->installEventFilter(this);
    labelCommentPix->installEventFilter(this);
    labelCollectionPix->installEventFilter(this);
}

void BlogItem::setPraise(bool isPraise, QString p)
{
    QPixmap pix, dst;
    labelPraise->setText(p);
    if(isPraise)
         pix = QPixmap(":/res/praise.jpeg");
    else
        pix = QPixmap(":/res/dispraise.jpeg"); // 缩放
    dst = pix.scaled(40,40,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelPraisePix->setPixmap(dst);
}
void BlogItem::setCollection(bool isCollection, QString c)
{
    QPixmap pix, dst;
    labelCollection->setText(c);
    if(isCollection)
        pix = QPixmap(":/res/collection.jpeg"); // 缩放
    else
        pix = QPixmap(":/res/discollection.jpeg");
    dst = pix.scaled(40,40,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelCollectionPix->setPixmap(QPixmap(dst));
}
void BlogItem::setComment(QString c)
{
    labelComment->setText(c);
}

// 事件过滤器，实现点击效果
bool BlogItem::eventFilter(QObject *obj, QEvent *event)
{
    // 鼠标进入
    if(event->type() == QEvent::Enter)
    {
        if(obj==labelTitle || obj==labelUser)
        {
            ((QLabel *)obj)->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
        }    
        else if(obj==labelCommentPix || obj==labelPraisePix || obj==labelCollectionPix)
        {
            ((QLabel *)obj)->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px;"));
        }
    }
    // 鼠标离开
    else if (event->type() == QEvent::Leave)
    {
        if(obj==labelTitle || obj==labelUser)
        {
            ((QLabel *)obj)->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");
        }
        else if(obj==labelCommentPix || obj==labelPraisePix || obj==labelCollectionPix)
        {
            ((QLabel *)obj)->setStyleSheet(QString::fromUtf8("border:0px solid blue; border-radius: 10px;"));
        }
    }
    // 鼠标单击事件
    else if(event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 左键单击
        if(mouseEvent->button() == Qt::LeftButton)
        {
            QMap<QString, QString> tmp;
            if(obj==labelTitle || obj==labelContent)
            {
                qDebug() << "label clicked0";
                // 发射信号,跳转到某个博客
                emit labelClicked(map, 0);
                return true;
            }
            else if(obj==labelUser)
            {
                qDebug() << "label clicked1";
                // 发射信号，跳转到某个作者
                emit labelClicked(map, 1);
                return true;
            }
            else if(obj==labelPraisePix)
            {
                qDebug() << "label clicked2";
                // 发射信号，跳转到某个作者
                emit labelClicked(map, 2);
                return true;
            }
            else if(obj==labelCollectionPix)
            {
                qDebug() << "label clicked3";
                // 发射信号，跳转到某个作者
                emit labelClicked(map, 3);
                return true;
            }
            else if(obj==labelCommentPix)
            {
                qDebug() << "label clicked4";
                // 发射信号，跳转到某个作者
                emit labelClicked(map, 4);
                return true;
            }
        }
        else
        {
            return false;
        }

    }
    return QObject::eventFilter(obj, event);
}

BlogItem::~BlogItem()
{
    delete ui;
}
